function getCookie(name) {
	var str = document.cookie;
	var arr = str.split('; ');
	for (var i = 0; i < arr.length; i++) {
		var c = arr[i].split('=');
		if (c[0] == name) {
			return c[1];
		}
	}
	return false;
}
function postMetaAjax(type, postid, varname, display_elm = null) {
	$.ajax({
		cache: false,
		url: ajax_object.ajax_url,
		type: 'POST',
		data: {
			'action': 'post_meta',
			'type': type,
			'post_id': postid,
		},
		dataType: 'JSON',
		async: true,
		success: function (result) {
			if (result.status == 'success') {
				if (display_elm) display_elm.text(result.data);
				var exdate = new Date();
				exdate.setDate(exdate.getDate() + 1);
				document.cookie = varname + '=' + result.data + '; expires=' + exdate.toGMTString() + '; path=/';
				if (type == 'add_likes') {
					document.cookie = varname + '_do' + '=1' + '; expires=' + exdate.toGMTString() + '; path=/';
				}
			}
		}
	});
}
function setPostmeta(postid) {
	var views_var = 'views_' + postid;
	var cookie_views = getCookie(views_var);
	var display_views = $('.post-meta .views');
	if (cookie_views) {
		display_views.text(cookie_views);
	} else {
		postMetaAjax('add_views', postid, views_var, display_views);
	}

	var display_like = $('.likes');
	var dolike_icon = $('#dolike .icon');
	var like_var = 'likes_' + postid;
	if (getCookie(like_var + '_do')) {
		dolike_icon.addClass('already');
	} else {
		dolike_icon.click(function (event) {
			event.preventDefault();
			if ($(this).hasClass('already') || getCookie(like_var + '_do')) return;
			dolike_icon.addClass('already');
			postMetaAjax('add_likes', postid, like_var, display_like);
		});
	}

	var cookie_like = getCookie(like_var);
	if (cookie_like) {
		display_like.text(cookie_like);
	} else {
		postMetaAjax('get_likes', postid, like_var, display_like);
	}

}